//! 节点的状态
//!
//!

use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy, Serialize, Deserialize)]
#[repr(u32)]
pub enum NodeStatus {
    HasInvalid = 0,
    WaitConsensus = 1,
    WaitUpdateInConsensus = 111,
    WaitUpdateHasConsensused = 112,
    WaitDeleteInConsensus = 121,
    WaitDeleteHasConsensused = 122,
    InConsensus = 2,
    UpdattingInConsensus = 211,
    UpdattingHasConsensused = 212,
    DelettingInConsensus = 221,
    DelettingHasConsensused = 222,
    HasConsensused = 3,
    HasShareded = 4,
}

impl Default for NodeStatus {
    fn default() -> Self {
        Self::HasInvalid
    }
}

impl Display for NodeStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeStatus::HasInvalid => f.write_str("HAS_INVALID"),
            NodeStatus::WaitConsensus => f.write_str("WAIT_CONSENSUS"),
            NodeStatus::WaitUpdateInConsensus => f.write_str("WAIT_UPDATE_IN_CONSENSUS"),
            NodeStatus::WaitUpdateHasConsensused => f.write_str("WAIT_UPDATE_HAS_CONSENSUSED"),
            NodeStatus::WaitDeleteInConsensus => f.write_str("WAIT_DELETE_IN_CONSENSUS"),
            NodeStatus::WaitDeleteHasConsensused => f.write_str("WAIT_DELETE_HAS_CONSENSUSED"),
            NodeStatus::InConsensus => f.write_str("IN_CONSENSUS"),
            NodeStatus::UpdattingInConsensus => f.write_str("UPDATTING_IN_CONSENSUS"),
            NodeStatus::UpdattingHasConsensused => f.write_str("UPDATTING_HAS_CONSENSUSED"),
            NodeStatus::DelettingInConsensus => f.write_str("DELETTING_IN_CONSENSUS"),
            NodeStatus::DelettingHasConsensused => f.write_str("DELETTING_HAS_CONSENSUSED"),
            NodeStatus::HasConsensused => f.write_str("HAS_CONSENSUSED"),
            NodeStatus::HasShareded => f.write_str("HAS_SHARDED"),
        }
    }
}

impl From<u32> for NodeStatus {
    fn from(value: u32) -> Self {
        match value {
            0 => Self::HasInvalid,
            1 => Self::WaitConsensus,
            111 => Self::WaitUpdateInConsensus,
            112 => Self::WaitUpdateHasConsensused,
            121 => Self::WaitDeleteInConsensus,
            122 => Self::WaitDeleteHasConsensused,
            2 => Self::InConsensus,
            211 => Self::UpdattingInConsensus,
            212 => Self::UpdattingHasConsensused,
            221 => Self::DelettingInConsensus,
            222 => Self::DelettingHasConsensused,
            3 => Self::HasConsensused,
            4 => Self::HasShareded,
            _ => panic!("unsupported status"),
        }
    }
}

impl Into<u32> for NodeStatus {
    fn into(self) -> u32 {
        self as u32
    }
}
